<template>
	<view class="center">
		<uni-sign-in ref="signIn"></uni-sign-in>
		<u-image width="100%" height="400rpx" mode="aspectFill" :src="topBg"></u-image>

		<view class="userInfo" @click.capture="toUserInfo">
			<!-- <uni-file-picker v-if="userInfo.avatar_file&&userInfo.avatar_file.url" v-model="userInfo.avatar_file"
				fileMediatype="image" :del-icon="false" return-type="object" :image-styles="listStyles" disablePreview
				disabled />
			<image v-else class="logo-img" src="/static/uni-center/defaultAvatarUrl.png"></image>
			<view class="logo-title">
				<text class="uer-name" v-if="hasLogin">{{userInfo.nickname||userInfo.username||userInfo.mobile}}</text>
				<text class="uer-name" v-else>未登陆</text>
			</view> -->

			<u-row style="width: 100%;padding: 0 10rpx 20rpx 30rpx;">
				<u-col span="12" align="bottom" style="padding: 0;">
					<u-icon name="setting" label="编辑资料" color="#f0f0f1" label-color="#f0f0f1" label-size="24"
						:custom-style="setIcon"></u-icon>
				</u-col>
			</u-row>
			<u-row style="width: 100%;padding: 0 30rpx;">
				<u-col span="2" text-align="left">
					<u-avatar v-if="hasLogin" :size="120" :show-level="true" level-icon="edit-pen-fill"
						level-bg-color="#2979ff" :src="getAvatar(userInfo)">
					</u-avatar>
					<u-avatar v-else :size="120" :show-level="true" level-icon="edit-pen-fill" level-bg-color="#d0d4da"
						src="/static/uni-center/defaultAvatarUrl.png">
					</u-avatar>
				</u-col>
				<u-col span="10" align="top" style="padding: 0 30rpx;">
					<u-row style="width: 100%;">
						<u-col span="12" align="top" style="padding: 0 10rpx 10rpx;">
							<text class="u-font-16 uer-name"
								v-if="hasLogin">{{userInfo.nickname||userInfo.username||userInfo.mobile}}</text>
							<text class="u-font-16 uer-name" v-else>未登陆</text>
						</u-col>
						<u-col span="12" align="top" style="padding: 0 10rpx 10rpx;">
							<view class="tagBox" style="background: rgb(158 158 158 / 49%)">
								<u-icon v-if="hasLogin" name="level" color="#2979ff" size="24" label-size="22"
									label-color="#ffffff" :label="userInfo.uth_status?userInfo.uth_status:'未认证'">
								</u-icon>
								<u-icon v-else name="level" color="#d0d4da" size="24" label="未认证" label-size="22"
									label-color="#ffffff"></u-icon>
							</view>
						</u-col>
						<u-col span="12" align="top">
							<text class="u-line-1 u-font-12 uer-des" v-if="hasLogin">
								{{getDes(userInfo.comment)}}
							</text>
							<text class="u-line-1 u-font-12 uer-des" v-else>
								{{nullDes.length>18?nullDes.substring(0,18)+'...':nullDes}}
							</text>
						</u-col>
					</u-row>
				</u-col>
			</u-row>
			<u-row style="width: 100%;padding: 20rpx 10rpx 0;color: #ffffff;">
				<u-col span="12" align="top">
					<u-row style="width: 100%;">
						<u-col span="3">
							<view class="u-font-14">3.60w</view>
							<view class="u-font-12 lightTxt">获赞</view>
						</u-col>
						<u-col span="3">
							<view class="u-font-14">7.22w</view>
							<view class="u-font-12 lightTxt">粉丝</view>
						</u-col>
						<u-col span="3">
							<view class="u-font-14">365</view>
							<view class="u-font-12 lightTxt">关注</view>
						</u-col>
						<u-col span="3">
							<view class="u-font-14">3.14w</view>
							<view class="u-font-12 lightTxt">收藏</view>
						</u-col>
					</u-row>
				</u-col>
			</u-row>
		</view>

		<!-- <uni-grid class="grid" :column="4" :showBorder="false" :square="true">
			<uni-grid-item class="item" v-for="(item,index) in gridList" @click.native="tapGrid(index)" :key="index">
				<uni-icons class="icon" color="#007AFF" :type="item.icon" size="26"></uni-icons>
				<text class="text">{{item.text}}</text>
			</uni-grid-item>
		</uni-grid> -->
		<u-row class="grid">
			<u-col span="3" style="padding: 20rpx 0;">
				<u-icon name="/static/uni-center/order.png" size="50" label="订单" label-size="26" label-pos="bottom">
				</u-icon>
			</u-col>
			<u-col span="3">
				<u-icon name="/static/uni-center/todo.png" size="50" label="代办" label-size="26" label-pos="bottom">
				</u-icon>
			</u-col>
			<u-col span="3">
				<u-icon name="/static/uni-center/money.png" size="50" label="资金" label-size="26" label-pos="bottom">
				</u-icon>
			</u-col>
			<u-col span="3">
				<u-icon @click="signIn()" name="/static/uni-center/signin.png" size="50" label="签到" label-size="26"
					label-pos="bottom">
				</u-icon>
			</u-col>
		</u-row>

		<uni-list class="center-list" v-for="(sublist , index) in ucenterList" :key="index">
			<uni-list-item v-for="(item,i) in sublist" :title="item.title" link :rightText="item.rightText" :key="i"
				:clickable="true" :to="item.to" @click="ucenterListClick(item)" :show-extra-icon="true"
				:extraIcon="{type:item.icon,color:'#999'}">
				<view v-if="item.showBadge" class="item-footer" slot="footer">
					<text class="item-footer-text">{{item.rightText}}</text>
					<view class="item-footer-badge"></view>
				</view>
			</uni-list-item>
		</uni-list>
	</view>
</template>

<script>
	import {
		mapGetters,
		mapMutations
	} from 'vuex';
	import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update';
	import callCheckVersion from '@/uni_modules/uni-upgrade-center-app/utils/call-check-version';
	import uniShare from '@/uni_modules/uni-share/js_sdk/uni-share.js';

	const db = uniCloud.database();
	export default {
		data() {
			return {
				nullDes: "更好的简介，可以让更多的小伙伴记住你！",
				topBg: "/static/topnav/navBg/bg1.png",
				setIcon: {
					"background": "rgba(158, 158, 158, 0.49)",
					"padding": "8rpx 12rpx",
					"border-radius": "100rpx"
				},
				gridList: [{
						"text": "订单",
						"icon": "order"
					},
					{
						"text": "文字2",
						"icon": "cloud-upload"
					},
					{
						"text": "文字3",
						"icon": "contact"
					},
					{
						"text": "文字4",
						"icon": "download"
					}
				],
				ucenterList: [
					[
						// {
						// 	"title": '签到有奖',
						// 	"event": 'signIn',
						// 	"icon": "compose"
						// },
						// #ifdef APP-PLUS
						{
							"title": '去评分',
							"event": 'gotoMarket',
							"icon": "hand-thumbsup"
						},
						//#endif
						{
							"title": '阅读过的文章',
							"to": '/pages/ucenter/read-news-log/read-news-log',
							"icon": "flag"
						},
						{
							"title": '我的积分',
							"to": '',
							"event": 'getScore',
							"icon": "paperplane"
						}
						// #ifdef APP-PLUS
						, {
							"title": '分销推荐',
							"event": 'share',
							"icon": "redo"
						}
						// #endif
					],
					[{
						"title": '问题与反馈',
						"to": '/uni_modules/uni-feedback/pages/uni-feedback/uni-feedback',
						"icon": "help"
					}, {
						"title": '设置',
						"to": '/pages/ucenter/settings/settings',
						"icon": "gear"
					}],
					[{
						"title": '关于',
						"to": '/pages/ucenter/about/about',
						"icon": "info"
					}]
				],
				listStyles: {
					"height": "150rpx", // 边框高度
					"width": "150rpx", // 边框宽度
					"border": { // 如果为 Boolean 值，可以控制边框显示与否
						"color": "#eee", // 边框颜色
						"width": "1px", // 边框宽度
						"style": "solid", // 边框样式
						"radius": "100%" // 边框圆角，支持百分比
					}
				}
			}
		},
		onLoad() {
			// console.log(313,this.userInfo,this.hasLogin);
			//#ifdef APP-PLUS
			this.ucenterList[this.ucenterList.length - 2].unshift({
				title: '检查更新',
				rightText: this.appVersion.version + '-' + this.appVersion.versionCode,
				event: 'checkVersion',
				icon: 'loop',
				showBadge: this.appVersion.hasNew
			})
			//#endif
		},
		computed: {
			...mapGetters({
				userInfo: 'user/info',
				hasLogin: 'user/hasLogin'
			})
			// #ifdef APP-PLUS
			,
			appVersion() {
				return getApp().appVersion
			}
			// #endif
			,
			appConfig() {
				return getApp().globalData.config
			}
		},
		methods: {
			...mapMutations({
				setUserInfo: 'user/login'
			}),
			getDes(_comment) {
				let _curComment = "";
				if (_comment) {
					_curComment = _comment;
				}
				if (_curComment.length > 0) {
					if (_curComment.length > 18) {
						_curComment = _curComment.substring(0, 18) + '...';
					}
				} else {
					_curComment = this.nullDes;
				}
				return _curComment;
			},
			getAvatar(_uinfo) {
				let _avatar = "";
				if (_uinfo.avatar_file && _uinfo.avatar_file.url) {
					_avatar = _uinfo.avatar_file.url + _uinfo.avatar_file.name + _uinfo.avatar_file.extname;
					console.log("_avatar:" + _avatar)
				} else {
					console.log("default_avatar")
					_avatar = '/static/uni-center/defaultAvatarUrl.png';
				}
				return _avatar;
				// return this.userInfo.avatar_file && this.userInfo.avatar_file.url ? this.userInfo.avatar_file :
				// 	'/static/uni-center/defaultAvatarUrl.png'
			},
			toSettings() {
				uni.navigateTo({
					url: "/pages/ucenter/settings/settings"
				})
			},
			signIn() { //签到
				// console.log("userInfo:" + JSON.stringify(this.userInfo));
				this.$refs.signIn.open()
			},
			/**
			 * 个人中心项目列表点击事件
			 */
			ucenterListClick(item) {
				if (!item.to && item.event) {
					this[item.event]();
				}
			},
			async checkVersion() {
				let res = await callCheckVersion()
				console.log("CheckVersion:" + res);
				if (res.result.code > 0) {
					checkUpdate()
				} else {
					uni.showToast({
						title: res.result.message,
						icon: 'none'
					});
				}
			},
			toUserInfo() {
				uni.navigateTo({
					url: '/pages/ucenter/userinfo/userinfo'
				})
			},
			tapGrid(index) {
				uni.showToast({
					title: '你点击了，第' + (index + 1) + '个',
					icon: 'none'
				});
			},
			/**
			 * 去应用市场评分
			 */
			gotoMarket() {
				// #ifdef APP-PLUS
				if (uni.getSystemInfoSync().platform == "ios") {
					// 这里填写appstore应用id
					let appstoreid = this.appConfig.marketId.ios; // 'id1417078253';
					plus.runtime.openURL("itms-apps://" + 'itunes.apple.com/cn/app/wechat/' + appstoreid + '?mt=8');
				}
				if (uni.getSystemInfoSync().platform == "android") {
					var Uri = plus.android.importClass("android.net.Uri");
					var uri = Uri.parse("market://details?id=" + this.appConfig.marketId.android);
					var Intent = plus.android.importClass('android.content.Intent');
					var intent = new Intent(Intent.ACTION_VIEW, uri);
					var main = plus.android.runtimeMainActivity();
					main.startActivity(intent);
				}
				// #endif
			},
			/**
			 * 获取积分信息
			 */
			getScore() {
				if (!this.userInfo) return uni.showToast({
					title: '请登录后查看积分',
					icon: 'none'
				});
				uni.showLoading({
					mask: true
				})
				db.collection("uni-id-scores").where('"user_id" == $env.uid').field('score,balance').get().then((res) => {
					uni.hideLoading()
					console.log(res);
					const data = res.result.data[0];
					let msg = '';
					msg = data ? ('当前积分为' + data.score) : '当前无积分';
					uni.showToast({
						title: msg,
						icon: 'none'
					});
				}).finally(() => {
					uni.hideLoading()
				})
			},
			async share() {
				let {
					result
				} = await uniCloud.callFunction({
					name: 'uni-id-cf',
					data: {
						action: 'getUserInviteCode'
					}
				})
				console.log(result);
				let myInviteCode = result.myInviteCode || result.userInfo.my_invite_code
				console.log(myInviteCode);
				let {
					appName,
					logo,
					company,
					slogan
				} = this.appConfig.about
				// #ifdef APP-PLUS
				uniShare({
					content: { //公共的分享类型（type）、链接（herf）、标题（title）、summary（描述）、imageUrl（缩略图）
						type: 0,
						href: this.appConfig.h5.url +
							`/#/pages/ucenter/invite/invite?code=uniInvitationCode:${myInviteCode}`,
						title: appName,
						summary: slogan,
						imageUrl: logo +
							'?x-oss-process=image/resize,m_fill,h_100,w_100' //压缩图片解决，在ios端分享图过大导致的图片失效问题
					},
					menus: [{
							"img": "/static/app-plus/sharemenu/wechatfriend.png",
							"text": "微信好友",
							"share": {
								"provider": "weixin",
								"scene": "WXSceneSession"
							}
						},
						{
							"img": "/static/app-plus/sharemenu/wechatmoments.png",
							"text": "微信朋友圈",
							"share": {
								"provider": "weixin",
								"scene": "WXSenceTimeline"
							}
						},
						{
							"img": "/static/app-plus/sharemenu/weibo.png",
							"text": "微博",
							"share": {
								"provider": "sinaweibo"
							}
						},
						{
							"img": "/static/app-plus/sharemenu/qq.png",
							"text": "QQ",
							"share": {
								"provider": "qq"
							}
						},
						{
							"img": "/static/app-plus/sharemenu/copyurl.png",
							"text": "复制",
							"share": "copyurl"
						},
						{
							"img": "/static/app-plus/sharemenu/more.png",
							"text": "更多",
							"share": "shareSystem"
						}
					],
					cancelText: "取消分享",
				}, e => { //callback
					console.log(e);
				})
				// #endif
			}
		}
	}
</script>

<style lang="scss" scoped>
	/* #ifndef APP-NVUE */
	view {
		display: flex;
		box-sizing: border-box;
		flex-direction: column;
	}

	page {
		background-color: #f8f8f8;
	}

	/* #endif*/

	.center {
		flex: 1;
		flex-direction: column;
		background-color: #f8f8f8;
	}

	.userInfo {
		width: 750rpx;
		padding: 40rpx 20rpx 34rpx;
		flex-direction: column;
		align-items: center;
		background: linear-gradient(0deg, rgba(0, 0, 0, 0.6), rgba(0, 0, 0, 0.01));
		margin: -360rpx 0 10rpx;
		z-index: 1;
	}

	.logo-img {
		width: 100rpx;
		height: 100rpx;
		border-radius: 150rpx;
	}

	.logo-title {
		flex: 1;
		align-items: center;
		justify-content: space-between;
		flex-direction: row;
	}

	.uer-name {
		color: #FFFFFF;
		font-weight: bold;
		letter-spacing: 4rpx;
	}

	.uer-des {
		color: #FFFFFF;
		letter-spacing: 2rpx;
	}

	.tagBox {
		padding: 2px 6px;
		border-radius: 7px;
	}

	.lightTxt {
		color: #d6d6d6;
	}

	.center-list {
		margin-bottom: 30rpx;
		background-color: #f9f9f9;
	}

	.center-list-cell {
		width: 750rpx;
		background-color: #007AFF;
		height: 40rpx;
	}

	.grid {
		background-color: #FFFFFF;
		border-radius: 16rpx 16rpx 0 0;
		margin: -20rpx 0 12rpx;
		z-index: 1;
	}

	.uni-grid .text {
		font-size: 30rpx;
		height: 25px;
		line-height: 25px;
		color: #817f82;
	}

	.uni-grid .item ::v-deep .uni-grid-item__box {
		justify-content: center;
		align-items: center;
	}


	/*修改边线粗细示例*/
	/* #ifndef APP-NVUE */
	.center-list ::v-deep .uni-list--border:after {
		-webkit-transform: scaleY(0.2);
		transform: scaleY(0.2);
		margin-left: 80rpx;
	}

	.center-list ::v-deep .uni-list--border-top,
	.center-list ::v-deep .uni-list--border-bottom {
		display: none;
	}

	/* #endif */
	.item-footer {
		flex-direction: row;
		align-items: center;
	}

	.item-footer-text {
		color: #999;
		font-size: 24rpx;
		padding-right: 10rpx;
	}

	.item-footer-badge {
		width: 20rpx;
		height: 20rpx;
		/* #ifndef APP-NVUE */
		border-radius: 50%;
		/* #endif */
		/* #ifdef APP-NVUE */
		border-radius: 10rpx;
		/* #endif */
		background-color: #DD524D;
	}
</style>
